package com.retailshop.shopuser.WapUserController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.retailshop.shopuser.common.*;
import com.retailshop.shopuser.entity.*;
import com.retailshop.shopuser.mapper.ShopRankMapper;
import com.retailshop.shopuser.mapper.UserOwnerMapper;
import com.retailshop.shopuser.service.UserNoService;
import com.retailshop.shopuser.service.UserOwnerService;
import com.retailshop.shopuser.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@CrossOrigin
@Controller
@RequestMapping("/wap")
public class WapUserOwnerController {

    @Autowired
    UserService userService;

    @Autowired
    UserNoService userNoService;

    @Autowired
    UserOwnerService userOwnerService;

    @Autowired
    UserOwnerMapper userOwnerMapper;

    @Autowired
    ShopRankMapper shopRankMapper;

    /**
     * 申请实习店主
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/internshipOwner")
    public Response internshipOwner(HttpServletRequest request) {
        int userNoId = ParameterUtil.getIntParameter(request.getParameter("userNoId"));
        if (userNoId>0) {
            OwnerUser ownerUser = userOwnerService.selectByPrimaryKey(userNoId);
            if (ownerUser != null) {
                if(ownerUser.getInternshipOwner())
                {
                    return new Response(MessageConstants.MSG_ITEM_EXISTS, null, 0, "你已经申请", request);
                }
            }

            OwnerUser userNo = new OwnerUser();
            userNo.setInternshipOwner(true);
            userNo.setInternshipTime(new Date());
            userNo.setId(userNoId);
            userOwnerMapper.setShopOwnerI(userNo);

            String name = request.getParameter("name");
            String cardId = request.getParameter("cardId");
            String cardIdPic1 = request.getParameter("cardIdPic1");
            String cardIdPic2 = request.getParameter("cardIdPic2");
            User user = new User();
            user.setName(name);
            user.setCardid(cardId);
            user.setId(ownerUser.getUserId());
            user.setCardIdPic1(cardIdPic1);
            user.setCardIdPic2(cardIdPic2);
            int row = userService.updateByPrimaryKeySelective(user);
            if (row > 0) {
                return new Response(MessageConstants.MSG_SUCCESS, row, 0, "", request);
            } else {
                return new Response(MessageConstants.MSG_SYS_ERROR, null, 0, "", request);
            }

        } else {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }

    }

    @ResponseBody
    @RequestMapping("/getOwnerInfo")
    public Response getOwnerInfo(HttpServletRequest request) {
        int userNoId = ParameterUtil.getIntParameter(request.getParameter("userNoId"));
        if(userNoId>0)
        {
            OwnerUser ownerUser = userOwnerService.selectByPrimaryKey(userNoId);
            if(ownerUser!=null)
            {
                String rankName="会员";
                switch (ownerUser.getRankId()) {
                    case 2:
                        //建立报表数据
                        if(ownerUser.getDataTime()==null) {
                            userOwnerService.InternshipLowerOwner(ownerUser);
                        }
                        rankName = "实习店主";
                        break;
                    case 3:
                        //建立报表数据
                        if(ownerUser.getDataTime()==null) {
                            userOwnerService.OwnerLowerOwner(ownerUser, 2);
                        }
                        rankName = "微店店主";
                        break;
                    case 7:
                        //建立报表数据
                        if(ownerUser.getDataTime()==null) {
                            userOwnerService.OwnerLowerOwner(ownerUser, 6);
                        }
                        rankName = "服务商";
                        break;
                    case 8:
                        //建立报表数据
                        if(ownerUser.getDataTime()==null) {
                            userOwnerService.OwnerLowerOwner(ownerUser, 7);
                        }
                        rankName = "服务中心";
                        break;
                    case 9:
                        //建立报表数据
                        if(ownerUser.getDataTime()==null) {
                            userOwnerService.OwnerLowerOwner(ownerUser, 8);
                        }
                        rankName = "总监";
                        break;
                    default:
                        break;
                }
                ownerUser.setShopRankList(shopRankMapper.selectList(ownerUser.getRankId()));
                ownerUser.setRankName(rankName);
                ownerUser.setInternshipOwner(null);
                ownerUser.setInternshipTime(null);
                ownerUser.setUserId(null);
                ownerUser.setDataTime(null);
                if(ownerUser.getDataTime()==null) {
                    OwnerUser ownerUser1 = userOwnerService.selectByPrimaryKey(userNoId);
                    ownerUser.setOrderUserCount(ownerUser1.getOrderUserCount());
                }
                return new Response(MessageConstants.MSG_SUCCESS, ownerUser, 1, "", request);
            }
            else
            {
                return new Response(MessageConstants.MSG_ITEM_NOT_EXIST, null, 0, "没有数据", request);
            }
        }
        return new Response(MessageConstants.MSG_NUMBER_FORMAT_ERROR, null, 0, "没有数据", request);
    }

    @ResponseBody
    @RequestMapping("/getOwnerInfo1")
    public Response getOwnerInfo1(HttpServletRequest request) {
        int userNoId = ParameterUtil.getIntParameter(request.getParameter("userNoId"));
        if(userNoId>0)
        {
            OwnerUser ownerUser = userOwnerService.selectByPrimaryKey(userNoId);
            if(ownerUser!=null)
            {
                String rankName="会员";
                switch (ownerUser.getRankId()) {
                    case 2:
                        rankName = "实习店主";
                        break;
                    case 3:
                        rankName = "微店店主";
                        break;
                    case 7:
                        rankName = "服务商";
                        break;
                    case 8:
                        rankName = "服务中心";
                        break;
                    case 9:
                        rankName = "总监";
                        break;
                    default:
                        break;
                }
                ownerUser.setRankName(rankName);
                ownerUser.setInternshipOwner(null);
                ownerUser.setInternshipTime(null);
                ownerUser.setUserId(null);
                ownerUser.setDataTime(null);
                ownerUser.setOrderAmount(null);
                ownerUser.setOrderUserCount(null);
                return new Response(MessageConstants.MSG_SUCCESS, ownerUser, 1, "", request);
            }
            else
            {
                return new Response(MessageConstants.MSG_ITEM_NOT_EXIST, null, 0, "没有数据", request);
            }
        }
        return new Response(MessageConstants.MSG_NUMBER_FORMAT_ERROR, null, 0, "没有数据", request);
    }

    @ResponseBody
    @RequestMapping("/getOwnerList")
    public Response getOwnerList(HttpServletRequest request) {
        int userNoId = ParameterUtil.getIntParameter(request.getParameter("userNoId"));
        if(userNoId>0)
        {
            int rankId = ParameterUtil.getIntParameter(request.getParameter("rankId"));
            int rankShopId = ParameterUtil.getIntParameter(request.getParameter("rankShopId"));
            int page=ParameterUtil.getIntParameter(request.getParameter("page")),limit=ParameterUtil.getIntParameter(request.getParameter("limit"));
            if(page==0)
            {
                page=1;
            }
            if(limit==0)
            {
                limit=5;
            }
            PageHelper.startPage(page, limit);
            List<OwnerShareUser> ownerShareUserList = userOwnerService.getOwnerList(userNoId,rankId,rankShopId);
            if(ownerShareUserList!=null) {
                PageInfo<OwnerShareUser> pageInfo = new PageInfo<>(ownerShareUserList);
                return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), pageInfo.getTotal(), "", request);
            }
            return new Response(MessageConstants.MSG_SUCCESS, null, 0, "", request);
        }
        return new Response(MessageConstants.MSG_NUMBER_FORMAT_ERROR, null, 0, "没有数据", request);
    }

    @ResponseBody
    @RequestMapping("/getFirstDay")
    public Response getFirstDay(HttpServletRequest request) {
        Date date = DateUtil.getNextDay();
        return new Response(MessageConstants.MSG_SUCCESS, date, 0, "", request);
    }


}
